#ifndef _morphology_h_
#define _morphology_h_

// morphology.h
//
// Image morphology, especially using min/max operators


#include "image/imageTmpl.h"
#include "image/imageTools.h"
#include "image/imageTypes.h"

namespace simple{
    //
    // morphology
    //
    template<class R, class T1, class T2, class S1, class S2>
    void _erode_cross (const R&, const Image<T1,S1>& src1,
                         const char* /*kernel*/, unsigned int /*size*/, int /*divisor*/,
                         Image<T2,S2>& dst1)
    {
        typename Image<T1,S1>::row_iterator i1 = src1.row_begin();
        typename Image<T2,S2>::row_iterator i2 = dst1.row_begin();

        unsigned int h = src1.height() - 2;
        unsigned int w = src1.width()  - 2;

        const T1* p1;
        T2* p2;
        unsigned int x, y;

        // Elements to skip between rows
        unsigned int pitch = (i1->bytes / sizeof (T1));

        for (y=0; y<h; y++, i1++, i2++) {
            p1 = i1->p + (pitch + 1); // Center the pixel
            p2 = i2->p;
            for (x=0; x<w; x++) {
                *p2++ = Min( Min( Min( Min(*p1, *(p1-1)), *(p1+1)), 
                                           *(p1-pitch)), *(p1+pitch));
                *p1++;
            }
        }
    }


    template<class T1, class S1>
    void erodeCross (const Image<T1,S1>& src1, 
                     Image<T1,S1>& dst1)
    {
        Function_s1d1Convolve<T1,T1,T1,S1,S1> processor (_erode_cross);

        char* kernel = 0;
        unsigned int size = 3;
        int divisor = 1;
        processor.run (src1, kernel, size, divisor, dst1);
    }
}

#endif // _morphology_h_
